package br.com.carser.mb;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import br.com.carser.business.ProfileBusiness;
import br.com.carser.business.UserBusiness;
import br.com.carser.exception.DuplicateEntityException;
import br.com.carser.exception.ParentEntityException;
import br.com.carser.mb.infra.BaseMB;
import br.com.carser.model.Profile;
import br.com.carser.model.User;
import br.com.carser.util.MessageUtil;

/**
 * Class to manager user methods.
 * 
 * @author Eduardo T. Serete
 */
@ManagedBean
@ViewScoped
public class UserManager extends BaseMB {

	private static final long serialVersionUID = 54983810443806014L;

	private static final String EXCLUDE_USER_SUCESS_MESSAGE = "faces.label.sucess.exclude.user.regitry";

	private static final String SAVE_USER_SUCESS_MESSAGE = "faces.label.sucess.save.user.regitry";

	private static final String MESSAGE_LOGIN_ERROR = "manager.message.login.dologin.failure";

	private static final String MESSAGE_LOGIN_SUCESS = "manager.message.login.dologin.success";

	private static final String MESSAGE_EMPTY_SEARCH = "faces.label.message.user.not.found";

	private static final String MESSAGE_DUPLICATE_INSERT_ERROR = "faces.label.validation.duplicate.user.data";

	private User user = new User();

	private String username;

	private String name;

	private String email;

	private String password;

	private String homePhone;

	private String cellPhone;

	private List<User> users;

	private Profile profile;
	
	private List<Profile> profiles;

	private boolean saved;

	@EJB
	private UserBusiness userBusinessBean;

	@EJB
	private ProfileBusiness profileBusiness;
	
	@PostConstruct
	public void init() {
		profiles = profileBusiness.getAll();
	}

	public void reset() {
		saved = false;
		homePhone = null;
		cellPhone = null;
		profile = new Profile();
		email = null;
		user = new User();
	}

	/**
	 * Create and save a new user.
	 * 
	 * @return String: action.
	 */
	public void save() {
		user.addEmail(email);
		user.addPhone(cellPhone);
		user.addPhone(homePhone);
		user.setProfile(profile);
		try {
			userBusinessBean.saveOrUpdate(user);
			MessageUtil.addFacesMessage(FacesMessage.SEVERITY_INFO, null,
					SAVE_USER_SUCESS_MESSAGE);
			saved = true;
		} catch (DuplicateEntityException e) {
			MessageUtil.addFacesMessage(FacesMessage.SEVERITY_ERROR, null,
					MESSAGE_DUPLICATE_INSERT_ERROR);
		}
	}

	/**
	 * Action to delete a user.
	 * @throws ParentEntityException 
	 */
	public void delete() throws ParentEntityException {
		userBusinessBean.delete(user);
		users.remove(user);
		MessageUtil.addFacesMessage(FacesMessage.SEVERITY_INFO, null,
				EXCLUDE_USER_SUCESS_MESSAGE);
	}

	/**
	 * Action to filter users.
	 * 
	 * @param name
	 * @param username
	 */
	public void search() {
		users = userBusinessBean.getByNameUsername(name, username);
		if (users.isEmpty()) {
			MessageUtil.addFacesMessage(FacesMessage.SEVERITY_INFO, null,
					MESSAGE_EMPTY_SEARCH);
		}
	}

	/**
	 * To user log in.
	 * 
	 * @return String: action.
	 */
	public String doLogin() {
		user = userBusinessBean.getByUserNamePassword(username, password);
		if (user == null) {
			MessageUtil.addFacesMessage(FacesMessage.SEVERITY_ERROR, null,
					MESSAGE_LOGIN_ERROR);
			return null;
		}
		MessageUtil.addFacesMessage(FacesMessage.SEVERITY_INFO, null,
				MESSAGE_LOGIN_SUCESS, user.getName());
		saveUserWithinSession(user);
		this.reset();
		return "user/userManager";
	}

	/**
	 * do Logoff.
	 */
	public String doLogoff() {
		((HttpSession) FacesContext.getCurrentInstance().getExternalContext()
				.getSession(true)).invalidate();
		return "index";
	}

	private void saveUserWithinSession(User user) {
		((HttpSession) FacesContext.getCurrentInstance().getExternalContext()
				.getSession(true)).setAttribute("userLogged", user);
	}

	/**
	 * @return the user
	 */
	public User getUser() {
		return user;
	}
	
	public void edit(User user) {
		this.user = userBusinessBean.getById(user.getId());
//		email = this.user.getEmails().get(0);
//		homePhone = this.user.getPhones().get(0);
//		cellPhone = this.user.getPhones().get(1);
		this.profile = user.getProfile();
	}

	/**
	 * @param user
	 *            the user to set
	 */
	public void setUser(User user) {
		this.user = user;
	}

	/**
	 * @return the email
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * @param email
	 *            the email to set
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * @return the homePhone
	 */
	public String getHomePhone() {
		return homePhone;
	}

	/**
	 * @param homePhone
	 *            the homePhone to set
	 */
	public void setHomePhone(String homePhone) {
		this.homePhone = homePhone;
	}

	/**
	 * @return the cellPhone
	 */
	public String getCellPhone() {
		return cellPhone;
	}

	/**
	 * @param cellPhone
	 *            the cellPhone to set
	 */
	public void setCellPhone(String cellPhone) {
		this.cellPhone = cellPhone;
	}

	/**
	 * @return the users
	 */
	public List<User> getUsers() {
		return users;
	}

	/**
	 * @param users
	 *            the users to set
	 */
	public void setUsers(List<User> users) {
		this.users = users;
	}

	/**
	 * @return the username
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * @param username
	 *            the username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password
	 *            the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}
	
	/**
	 * @return the profile
	 */
	public Profile getProfile() {
		return profile;
	}

	/**
	 * @param profile the profile to set
	 */
	public void setProfile(Profile profile) {
		this.profile = profile;
	}

	/**
	 * @return the saved
	 */
	public boolean isSaved() {
		return saved;
	}

	/**
	 * @param saved
	 *            the saved to set
	 */
	public void setSaved(boolean saved) {
		this.saved = saved;
	}

	/**
	 * @return the profiles
	 */
	public List<Profile> getProfiles() {
		return profiles;
	}

	/**
	 * @param profiles the profiles to set
	 */
	public void setProfiles(List<Profile> profiles) {
		this.profiles = profiles;
	}

}